/**
 * @(#) popHouseSavingMediator.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 탭6 의료비 공제 팝업창 Logic을 관리 
 */
 package com.skt.common_hr.yearadjustment.view
 {
 	import com.skt.common_hr.common.PsHrAlert;
 	import com.skt.common_hr.common.Variables;
 	import com.skt.common_hr.common.puremvc.CommonMediator;
 	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
 	import com.skt.common_hr.yearadjustment.model.BankListProxy;
 	import com.skt.common_hr.yearadjustment.model.HouseSavingProxy;
 	import com.skt.common_hr.yearadjustment.model.vo.HouseSavingVO;
 	import com.skt.common_hr.yearadjustment.view.components.popHouseSaving;
 	import com.skt.common_hr.yearadjustment.view.components.popMedicalSubtraction;
 	import com.wcl.utils.DateUtil;
 	
 	import flash.events.Event;
 	
 	import mx.managers.PopUpManager;
 	
 	import org.puremvc.as3.multicore.interfaces.INotification;
 	
 	
 	public class popHouseSavingMediator extends CommonMediator
 	{
 		public static const NAME:String = "popHouseSavingMediator";
 		public var v_year:String = "";
		[Bindable] public var v_enable:Boolean = true;
//shgoo 2010.01.12
		//추가, 수정 구분
		private var bAdd:Boolean = true;
				 		
 		/**
 		 * 생성자
 		 * 
 		 * @param
 		 * @return void
 		 * */
 		public function popHouseSavingMediator( viewComponent:Object ):void
 		{
 			super(NAME, viewComponent);
 			view.callLater(initView);
 		}
 		
 		/**
 		 * View를 초기화 한다.
 		 * 	- View에 기본사항을 출력하고 data를 load
 		 * @param
 		 * @return void
 		 * */
 		public function initView():void
 		{
 			view.addEventListener(popMedicalSubtraction.OkInput, onClickSave);
 			view.addEventListener(popMedicalSubtraction.CancelInput, onCancelInput);

 			view._isEnable = v_enable;
 			loadHouseSavingTypeList();
 			loadBankList();
 		}
 
///////////////////
 		
 		
 		//기본 데이터 조회
 		private function loadHouseSavingList():void{
 			var proxy:HouseSavingProxy = facade.retrieveProxy(HouseSavingProxy.NAME) as HouseSavingProxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		private function loadBankList():void{
 			var proxy:BankListProxy= facade.retrieveProxy(BankListProxy.NAME) as BankListProxy;
 			proxy.loadBankList();
 		}
 		

 		private function loadHouseSavingTypeList():void{
 			var proxy:HouseSavingProxy = facade.retrieveProxy(HouseSavingProxy.NAME) as HouseSavingProxy;
 			proxy.loadHouseSavingType(v_year);
 		}
 		
 		//저장
 		private function onClickSave(event:Event):void{
 			
 			var savingList:Array = new Array();
	 		for each(var rec:HouseSavingVO in view.houseSavingList)
	 		{
	 			var obj:Object = new Object();
				obj.PREIN = rec.PREIN;			//종(전)근무지
				obj.PNSTY = rec.PNSTY;			//비용의 2차 범주
				obj.PNSTT = rec.PNSTT;			
				obj.BETRG = rec.BETRG;			//금액
				obj.FINCO = rec.FINCO;			//금융기관코드
				obj.INSNM = rec.INSNM;			//연금저축 금융사명
				obj.ACCNO = rec.ACCNO;			//계좌번호
				obj.BEGDA = DateUtil.toBasicString(rec.BEGDA);			//시작일
				obj.ENDDA = DateUtil.toBasicString(rec.ENDDA);			//종료일
	 			
	 			savingList.push(obj);
	 		}
 			
 			var proxy:HouseSavingProxy = facade.retrieveProxy(HouseSavingProxy.NAME) as HouseSavingProxy;
 			proxy.save(Variables.yearPernr, v_year, savingList);
 		}
 		
 		//팝업 닫기
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popHouseSavingMediator.NAME);
 			PopUpManager.removePopUp(view);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					HouseSavingProxy.LOAD_HOUSE_SAVING_COMPLETE,
					HouseSavingProxy.LOAD_HOUSE_SAVING_FAILED,
					HouseSavingProxy.LOAD_HOUSE_TYPE_LIST_COMPLETE,
					HouseSavingProxy.LOAD_HOUSE_TYPE_LIST_FAILED,
					HouseSavingProxy.SAVE_HOUSE_SAVING_COMPLETE,
					HouseSavingProxy.SAVE_HOUSE_SAVING_FAILED,
					BankListProxy.LOAD_BANK_LIST_COMPLETE,
					BankListProxy.LOAD_BANK_LIST_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case HouseSavingProxy.LOAD_HOUSE_SAVING_COMPLETE:
					view.houseSavingList = (note.getBody() as HouseSavingProxy).houseSavingList;
					break;
				case HouseSavingProxy.LOAD_HOUSE_SAVING_FAILED:
					PsHrAlert.showMessage((note.getBody() as HouseSavingProxy).msgStr);
					break;
				case HouseSavingProxy.LOAD_HOUSE_TYPE_LIST_COMPLETE:
					view.houseSavingTypeList = (note.getBody() as HouseSavingProxy).houseSavingTypeList;
					loadHouseSavingList();
					break;
				case HouseSavingProxy.SAVE_HOUSE_SAVING_COMPLETE:
					onCancelInput(null);
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
					//facade.sendNotification(YearAdjustmentView.CHECK_REQ_MEDICALSUBTRACTION);
					break;
				case HouseSavingProxy.SAVE_HOUSE_SAVING_FAILED:
					PsHrAlert.showMessage((note.getBody() as HouseSavingProxy).msgStr);
					break;
				case BankListProxy.LOAD_BANK_LIST_COMPLETE:
					view.bankList = (note.getBody() as BankListProxy).bankList;
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function get view():popHouseSaving
		{
			return viewComponent as popHouseSaving;
		}
	}
 }